package com.shenfa.action;

import java.util.List;

import org.apache.log4j.Logger;
import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;
import com.shenfa.bean.bill.rule.DimensionInfo;
import com.shenfa.service.DimensionInfoService;

import com.shenfa.util.ResultObject;

public class DimensionInfoAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	// 返回结果集对象
	public JSONObject result;
	// 分页，第几条数据
	private int start;
	// 每页显示数据条数
	private int limit;
	// 每页显示数据总条数
	private int total;
	// 接收页面数据
	private DimensionInfo dimen;
	private static Logger log=Logger.getLogger(DimensionInfoAction.class);
	// service
	@Autowired
	private DimensionInfoService dimensionInfoService;

	@SuppressWarnings("unchecked")
	public String dimenGridList() {
		log.debug("start="+start+",limit="+limit);
		List<DimensionInfo> list = dimensionInfoService.doGridList(start, limit);
		total = dimensionInfoService.doDimenPageTotal();
		result = ResultObject.build("0", list);
		result.put("total",total);
		return SUCCESS;
	}

	public String addDimension(){
		log.debug("field="+dimen.getField()+",fieldName="+dimen.getField_name()+"fieldType"+dimen.getField_type());
		boolean res = dimensionInfoService.addDimension(dimen);
		result = ResultObject.build("0", res);
		return SUCCESS;
	}
	
	public String editDimension(){
		log.debug("field="+dimen.getField()+",fieldName="+dimen.getField_name()+",fieldType="+dimen.getField_type());
		boolean res = dimensionInfoService.editDimension(dimen);
		result = ResultObject.build("0", res);
		return SUCCESS;
	}
	
	public String deleteDimension(){
		log.debug("fieldType="+dimen.getField_type());
		boolean res = dimensionInfoService.deleteDimension(dimen.getField_type());
		result = ResultObject.build("0", res);
		return SUCCESS;
	}
	
	public String validtateField_type(){
		if(null!=dimen){
			log.debug("fieldType="+dimen.getField_type());
			int flag = dimensionInfoService.validateField(dimen.getField_type());
			result = ResultObject.build(String.valueOf(flag), null);
		}
		return SUCCESS;
	}
	
	/**
	 * 分页处理，起始条数
	 * 
	 * @param start
	 */
	public void setStart(int start) {
		this.start = Math.abs(start);
	}

	/**
	 * 分页处理，每页数据条数
	 * 
	 * @param limit
	 */
	public void setLimit(int limit) {
		this.limit = Math.abs(limit);
	}

	public DimensionInfo getDimen() {
		return dimen;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public void setDimen(DimensionInfo dimen) {
		this.dimen = dimen;
	}

}
